package me.youline.dataServer.service.support;

import me.youline.dataServer.entity.UserDeviceToken;
import me.youline.dataServer.service.UserDeviceTokenService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.notnoop.apns.ApnsService;

/**
 * 移动端推送业务类
 * @author linhan
 *
 */
@Component
public class MobilePushService{
	
	@Autowired
	private ApnsService service ;
	
	@Autowired
	private UserDeviceTokenService userDeviceTokenService; 
	
	/**
	 * 向指定用户推送消息，若用户当前未登陆则不推送。
	 * 
	 * @param userId
	 * @param payload
	 */
	public void push(Long userId,String payload){
		UserDeviceToken deviceToken = userDeviceTokenService.getUserDeviceToken(userId);
		if(deviceToken != null && deviceToken.isValidDeviceToken()){
			service.push(deviceToken.getDeviceToken(), payload);
		}
	}
	
}